home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 151-175 / scopedisk171 / pclock / pclock.asm < prev    next >
Assembly Source File  |  1995-03-19  |  10KB  |  392 lines

  1.  
  2. ;       PClock V1.0
  3. ;    by Preben Nielsen.
  4. ;    Assemble it as case-sensitive.
  5. ;    OPT O+
  6. ;    OPT O1+        ;Tells when a branch could be optimised to short
  7. ;    OPT i+        ;Tells when '#' is probably missing
  8.  
  9. ;TWELVE_HOURS        ;Uncomment this line if you don't want
  10.             ;a 24-hour clock
  11.  
  12.     incdir    "INCLUDE:"
  13.     include "devices/timer.i"
  14.     include "exec/memory.i"
  15.     include "exec/devices.i"
  16.     include "exec/exec_lib.i"
  17.     include "graphics/graphics_lib.i"
  18.     include "intuition/intuition.i"
  19.     include "intuition/intuition_lib.i"
  20.     include "libraries/dosextens.i"
  21.     include "libraries/dos_lib.i"
  22.  
  23. ;TWELVE_HOURS        ;Uncomment this line if you don't want
  24.             ;a 24-hour clock
  25.  
  26. * A few macro's
  27. LoadBase    MACRO
  28.         IFNC        '\1','ExecBase'
  29.         movea.l        \1(A5),A6
  30.         ENDC
  31.         IFC        '\1','ExecBase'
  32.         movea.l        4.W,A6
  33.         ENDC
  34.         ENDM
  35. CallLib        MACRO
  36.         jsr        _LVO\1(A6)
  37.         ENDM
  38. Call        MACRO
  39.         bsr        \1
  40.         ENDM
  41. Push        MACRO
  42.         movem.l        \1,-(SP)
  43.         ENDM
  44. Pop        MACRO
  45.         movem.l        (SP)+,\1
  46.         ENDM
  47.  
  48. * Offsets into datablock
  49. WBenchMsg    =0
  50. DosBase        =4
  51. GfxBase        =8
  52. IntBase        =12
  53. TimerPort    =16
  54. TimerOpen    =20
  55. Counter        =22
  56. CWindow        =24
  57. Rp        =28
  58. Up        =32
  59. Stamp        =36        ;\
  60. Days        =36        ; \struct DateStamp Stamp;
  61. Minutes        =40        ; /
  62. Ticks        =44        ;/
  63. TimeReq        =48        ;struct TimeRequest TimeReq; (Size IOTV_SIZE)
  64. RelDataSize    =48+4+IOTV_SIZE
  65.  
  66.         SECTION CLOCKCODE,CODE
  67. DBase        EQUR        A5
  68. PClock        Push        D0-D7/A0-A6
  69.         lea        -RelDataSize(SP),SP    ;Make room on stack for data
  70.         movea.l        SP,DBase
  71.         moveq        #RelDataSize-1,D0
  72. ClrData        clr.b        (DBase)+        ;Clear data area
  73.         dbf        D0,ClrData
  74.         movea.l        SP,DBase
  75.         LoadBase    ExecBase
  76.         suba.l        A1,A1
  77.         CallLib        FindTask        ;Find us
  78.         movea.l        D0,A2
  79.         tst.l        pr_CLI(A2)        ;pr_CLI
  80.         bne.S        CLIAndWBStartup
  81. WBenchStartup    lea        pr_MsgPort(A2),A0    ;pr_MsgPort
  82.         CallLib        WaitPort        ;Wait for a message
  83.         lea        pr_MsgPort(A2),A0
  84.         CallLib        GetMsg            ;Then get it
  85.         move.l        D0,WBenchMsg(DBase)    ;Save it for later reply
  86. CLIAndWBStartup    lea        DosName(PC),A1
  87.         CallLib        OldOpenLibrary        ;Open dos
  88.         move.l        D0,DosBase(DBase)
  89.         beq.S        Exit
  90.         lea        GfxName(PC),A1
  91.         CallLib        OldOpenLibrary        ;Open graphics
  92.         move.l        D0,GfxBase(DBase)
  93.         beq.S        Exit
  94.         lea        IntuiName(PC),A1
  95.         CallLib        OldOpenLibrary        ;Open Intuition
  96.         move.l        D0,IntBase(DBase)
  97.         beq.S        Exit
  98.         lea        FileNW(PC),A0
  99.         LoadBase    IntBase
  100.         CallLib        OpenWindow        ;Open the Window
  101.         move.l        D0,CWindow(DBase)
  102.         beq.S        Exit
  103.         movea.l        D0,A0
  104.         move.l        50(A0),Rp(DBase)    ;Get RastPort
  105.         move.l        86(A0),Up(DBase)    ;Get UserPort
  106.         suba.l        A1,A1
  107.         lea        ScrTitle(PC),A2
  108.         CallLib        SetWindowTitles
  109.         lea        TxtAttr(PC),A0
  110.         LoadBase    GfxBase
  111.         CallLib        OpenFont
  112.         tst.l        D0
  113.         beq.S        Exit
  114.         movea.l        D0,A0
  115.         movea.l        Rp(DBase),A1
  116.         CallLib        SetFont            ;Have to be sure to use the right size of font
  117.         moveq        #0,D0
  118.         movea.l        Rp(DBase),A1
  119.         CallLib        SetAPen
  120.         moveq        #1,D0
  121.         movea.l        Rp(DBase),A1
  122.         CallLib        SetBPen
  123.         Call        OpenTimer
  124.         beq.S        Main            ;Lets begin
  125. * Here we exit
  126. Exit        Call        CloseTimer
  127. CloseWin    LoadBase    IntBase
  128.         move.l        CWindow(DBase),D0
  129.         beq.S        CloseIntui
  130.         movea.l        D0,A0
  131.         CallLib        CloseWindow        ;Close window if it is open
  132. CloseIntui    LoadBase    ExecBase
  133.         move.l        IntBase(DBase),D0
  134.         beq.S        CloseGfx
  135.         movea.l        D0,A1
  136.         CallLib        CloseLibrary        ;Close intuition if it is open
  137. CloseGfx    move.l        GfxBase(DBase),D0
  138.         beq.S        CloseDos
  139.         movea.l        D0,A1
  140.         CallLib        CloseLibrary        ;Close graphics if it is open
  141. CloseDos    move.l        DosBase(DBase),D0
  142.         beq.S        ReplyWB
  143.         movea.l        D0,A1
  144.         CallLib        CloseLibrary        ;Close graphics if it is open
  145. ReplyWB        move.l        WBenchMsg(DBase),D2
  146.         beq.S        AllDone
  147.         CallLib        Forbid
  148.         movea.l        D2,A1
  149.         CallLib        ReplyMsg        ;Reply WBenchMessage if we are started from WB
  150. AllDone        lea        RelDataSize(DBase),SP    ;Restore SP
  151.         Pop        D0-D7/A0-A6
  152.         moveq        #0,D0
  153.         rts
  154.  
  155. * Here we really begin    
  156. Main
  157. StartTimer    Call        WriteStr
  158. EventLoop    LoadBase    ExecBase
  159.         moveq        #0,D0
  160.         moveq        #0,D1
  161.         movea.l        Up(DBase),A0
  162.         move.b        MP_SIGBIT(A0),D1
  163.         bset        D1,D0
  164.         movea.l        TimerPort(DBase),A0
  165.         move.b        MP_SIGBIT(A0),D1
  166.         bset        D1,D0
  167.         CallLib        Wait
  168.         movea.l        TimerPort(DBase),A0
  169.         CallLib        GetMsg            ;Message from Timer
  170.         tst.l        D0
  171.         bne.S        StartTimer
  172. GetNextMsg    movea.l        Up(DBase),A0        ;Has to be message from intuition
  173.         CallLib        GetMsg            ;Get the message
  174.         tst.l        D0
  175.         beq.S        EventLoop        ;Did we get a message
  176.         movea.l        D0,A1            ;Yes, put message address to A1 and reply
  177.         move.l        20(A1),D7
  178.         CallLib        ReplyMsg        ;Reply the message
  179.         cmpi.l        #CLOSEWINDOW,D7
  180.         beq        Exit
  181.         bra.S        GetNextMsg
  182.  
  183. ;Writes available memory, writes time, and sends a timer-request
  184. WriteStr    Push        D0-D7/A0-A6
  185.         LoadBase    DosBase
  186.         lea        Stamp(DBase),A0
  187.         move.l        A0,D1
  188.         CallLib        DateStamp        ;Get current time
  189.         Call        StampToStr
  190.         subq.w        #1,Counter(DBase)
  191.         bgt.S        DontUpdateMem
  192.         move.w        #4,Counter(DBase)
  193.         Call        MemToStr        ;Updates memory every 4 seconds
  194. DontUpdateMem    LoadBase    GfxBase
  195.         moveq        #30,D0
  196.         moveq        #7,D1
  197.         movea.l        Rp(DBase),A1
  198.         CallLib        Move
  199.         moveq        #36,D0
  200.         lea        AllStr(PC),A0
  201.         movea.l        Rp(DBase),A1
  202.         CallLib        Text
  203.         Call        SendTimerReq
  204.         Pop        D0-D7/A0-A6
  205.         rts
  206.  
  207. StampToStr    Push        D0-D7/A0-A6
  208.         move.l        Minutes(DBase),D2
  209.         divu        #60,D2
  210.         move.l        D2,D1
  211.         swap        D1
  212.     IFD TWELVE_HOURS
  213.         ext.l        D2
  214.         divu        #12,D2
  215.         swap        D2
  216.     ENDC
  217.         move.l        Ticks(DBase),D0
  218.         divu        #50,D0
  219.         move.w        D0,-(SP)        ;Seconds
  220.         move.w        D1,-(SP)        ;Minutes
  221.         move.w        D2,-(SP)        ;Hours
  222.         pea        TimeFormat
  223.         pea        TimeStr            ;Destination
  224.         Call        Format            ;sprintf(fmt,Destination,Hour,Minute,Second);
  225.         lea        14(SP),SP
  226.         Pop        D0-D7/A0-A6
  227.         rts
  228.  
  229. MemToStr    Push        D0-D7/A0-A6
  230.         LoadBase    ExecBase
  231.         moveq        #MEMF_CHIP,D1
  232.         CallLib        AvailMem    ;AvailMem(MEMF_CHIP);
  233.         move.l        D0,-(SP)
  234.         pea        MemFormat(PC)
  235.         pea        ChipStr(PC)
  236.         Call        Format
  237.         lea        12(SP),SP
  238.         moveq        #MEMF_FAST,D1
  239.         CallLib        AvailMem    ;AvailMem(MEMF_FAST);
  240.         move.l        D0,-(SP)
  241.         pea        MemFormat(PC)
  242.         pea        FastStr(PC)
  243.         Call        Format
  244.         lea        12(SP),SP
  245.         lea        AllStr(PC),A0
  246.         move.b        #' ',13(A0)
  247.         move.b        #' ',27(A0)
  248.         Pop        D0-D7/A0-A6
  249.         rts
  250.  
  251. * ----------------------- Misc sub-routines ----------------------------
  252. ;Format(Buffer,Format-string,datastream);
  253. Format        Push        D0-D1/A0-A3
  254.         move.l        4*6+4(SP),A3    ;Destination buffer
  255.         move.l        4*6+8(SP),A0    ;Format-string
  256.         lea        4*6+12(SP),A1    ;Data on stack
  257.         lea        PutChar(PC),A2    ;Function
  258.         LoadBase    ExecBase
  259.         CallLib        RawDoFmt
  260.         Pop        D0-D1/A0-A3
  261.         rts
  262. PutChar        move.b        D0,(A3)+
  263.         rts
  264.  
  265. ;A0=Port Name, D0=Priority
  266. ;Returns 0 if it failed address of port if it had success
  267. CreatePort    Push        D1-D7/A0-A6
  268.         LoadBase    ExecBase
  269.         move.l        D0,D5
  270.         movea.l        A0,A5
  271.         moveq        #-1,D0
  272.         CallLib        AllocSignal
  273.         move.b        D0,D6
  274.         bmi.S        CPortFailed            ;AllocSignal returned -1
  275.         moveq        #MP_SIZE,D0
  276.         move.l        #MEMF_PUBLIC!MEMF_CLEAR,D1
  277.         CallLib        AllocMem
  278.         move.l        D0,D7
  279.         beq.S        CNoMemory
  280.         movea.l        D0,A2
  281.         move.l        A5,MP+LN_NAME(A2)        ;MsgPort->mp_Node.ln_Name=Name;
  282.         move.b        D5,MP+LN_PRI(A2)        ;MsgPort->mp_Node.ln_Pri =Pri;
  283.         move.b        #NT_MSGPORT,MP+LN_TYPE(A2)    ;MsgPort->mp_Node.ln_Type=NT_MSGPORT;
  284.         clr.b        MP_FLAGS(A2)            ;MsgPort->mp_Flags     =PA_SIGNAL;
  285.         move.b        D6,MP_SIGBIT(A2)        ;MsgPort->mp_SigBit     =MPSigBit;
  286.         suba.l        A1,A1
  287.         CallLib        FindTask
  288.         move.l        D0,MP_SIGTASK(A2)        ;MsgPort->mp_SigTask     =FindTask(0);
  289.         movea.l        A2,A1
  290.         CallLib        AddPort
  291.         move.l        D7,D0
  292.         bra.S        DoneCPort
  293. CNoMemory    move.b        D6,D0
  294.         CallLib        FreeSignal
  295. CPortFailed    moveq        #0,D0
  296. DoneCPort    Pop        D1-D7/A0-A6            ;Returns 0 or address of port
  297.         rts
  298. ;A1=MsgPort
  299. DeletePort    Push        D0-D7/A0-A6
  300.         LoadBase    ExecBase
  301.         movea.l        A1,A2
  302.         CallLib        RemPort
  303.         moveq        #0,D0
  304.         move.b        MP_SIGBIT(A2),D0
  305.         CallLib        FreeSignal
  306.         movea.l        A2,A1
  307.         moveq        #MP_SIZE,D0
  308.         CallLib        FreeMem
  309.         Pop        D0-D7/A0-A6
  310.         rts
  311.  
  312. ;Send a timer request for 1 second
  313. SendTimerReq    Push        D0-D1/A0-A1/A6
  314.         LoadBase    ExecBase
  315.         lea        TimeReq(DBase),A1
  316.         move.l        TimerPort(DBase),IO+MN_REPLYPORT(A1);TimeReq.tr_node.io_Message.mn_ReplyPort=Rep;
  317.         move.w        #TR_ADDREQUEST,IO_COMMAND(A1)    ;TimeReq.tr_node.io_Command        =TR_ADDREQUEST;
  318.         clr.b        IO_FLAGS(A1)            ;TimeReq.tr_node.io_Flags        =0;
  319.         clr.b        IO_ERROR(A1)            ;TimeReq.tr_node.io_Error        =0;
  320.         move.l        #1,IOTV_TIME+TV_SECS(A1)    ;TimeReq.tr_time.tv_Secs        =1;
  321.         clr.l        IOTV_TIME+TV_MICRO(A1)        ;TimeReq.tr_time.tv_Micros        =0;
  322.         CallLib        SendIO
  323.         Pop        D0-D1/A0-A1/A6
  324.         rts
  325.  
  326. ;Creates port and opens timer.device
  327. ;Returns -1 if it failed and 0 if it had success
  328. OpenTimer    Push        D1/A0-A1/A6
  329.         lea        PortName(PC),A0
  330.         moveq        #0,D0                ;Priority
  331.         Call        CreatePort
  332.         move.l        D0,TimerPort(DBase)
  333.         beq.S        OTimerFailed
  334.         lea        TimerName(PC),A0
  335.         lea        TimeReq(DBase),A1
  336.         moveq        #UNIT_VBLANK,D0
  337.         moveq        #0,D1
  338.         LoadBase    ExecBase
  339.         CallLib        OpenDevice
  340.         tst.l        D0
  341.         beq.S        DoneOTimer
  342.         movea.l        TimerPort(DBase),A1
  343.         Call        DeletePort
  344. OTimerFailed    moveq        #-1,D0
  345. DoneOTimer    move.w        D0,TimerOpen(DBase)        ;0 or -1
  346.         Pop        D1/A0-A1/A6
  347.         rts
  348.  
  349. ;Deletes port and closes timer.device
  350. CloseTimer    Push        D0-D1/A0-A1/A6
  351.         tst.w        TimerOpen(DBase)
  352.         bne.S        DoneCloseTimer
  353.         LoadBase    ExecBase
  354.         lea        TimeReq(DBase),A1
  355.         CallLib        AbortIO
  356.         lea        TimeReq(DBase),A1
  357.         CallLib        CloseDevice
  358.         movea.l        TimerPort(DBase),A1
  359.         Call        DeletePort
  360. DoneCloseTimer    Pop        D0-D1/A0-A1/A6
  361.         rts
  362.  
  363. DosName        dc.b        'dos.library',0
  364. GfxName        dc.b        'graphics.library',0
  365. IntuiName    dc.b        'intuition.library',0
  366. TimerName    dc.b        'timer.device',0
  367. PortName    dc.b        'PClock Port',0
  368.         EVEN
  369.  
  370. FileNW        dc.w        217,0,373,10
  371.         dc.b        0,1
  372.         dc.l        CLOSEWINDOW,WINDOWCLOSE!WINDOWDRAG!WINDOWDEPTH!RMBTRAP,0,0,0,0,0
  373.         dc.w        0,0,0,0,WBENCHSCREEN
  374.  
  375. TxtAttr        dc.l        FontName
  376.         dc.w        TOPAZ_EIGHTY
  377.         dc.b        FS_NORMAL,FPB_ROMFONT
  378.  
  379. FontName    dc.b        'topaz.font',0
  380.  
  381. ScrTitle    dc.b        'PClock V1.0 by Preben Nielsen in 1990. This is Public Domain',0
  382.  
  383. MemFormat    dc.b        '%8ld',0
  384. TimeFormat    dc.b        '%02d:%02d:%02d',0
  385.  
  386. AllStr        dc.b        'Chip:'
  387. ChipStr        dc.b        '       0 Fast:'
  388. FastStr        dc.b        '       0 '
  389. TimeStr        dc.b        '12:12:12 '
  390.         END
  391.  
  392.